// miniprogram/pages/show/show.js
const db = wx.cloud.database()
const substance = db.collection('substance')
const user = db.collection('user')
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesIdx:0,
    searchValue:null,
    columnsBook: ['计算机', '机电', '自动化', '轻化', '信工', '土木与交通', '管理', '材能', '环境', '外国语', '应数', '物理', '艺设', '政法', '建筑城规', '经贸', '生物医学', '国际教育'],
    columnsGood: ['电子产品', '体育用品', '生活用品', '其他'],
    goodList:null,
    freelist:null,
    "bnrUrl": [{
      "url": "../../images/addgood.jpg"
    }, {
        "url": "../../images/addbook.jpg"
    }, {
        "url": "../../images/content.png"
    }]
  }, 
  onChangeV:function(e){
    this.setData({
      searchValue: e.detail
    });
  },
  onSearch:function(e){
    let self=this
    console.log(e)
    wx.navigateTo({
      url: '../show/search/search?seach=' + self.data.searchValue,
    }) 
    this.setData({
      searchValue: null
    });
  },
  refresh: function (e) {
    let self = this
    self.setData({
      refresh: '1',
      hideAfter: '',
    })
    self.onShow();
    setTimeout(function () {
      self.setData({
        refresh: '',
      })
    }, 800)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    let self=this;
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: function (res) {
          }, fail: res => {
            wx.reLaunch({
              url: '../index/index'
            })
          }
        })
      },
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let self = this;
    substance.where({
      free: null
    })
      // .orderBy("date").skip(self.data.pagesIdx).limit(5)
      .get({
        success: res => {
          let goodList = res.data
          let handleTime = (date) => {
            let nowTime = (new Date()).valueOf()
            if (parseInt((nowTime - date) / 2592000000) >= 1) {
              return parseInt((nowTime - date) / 2592000000) + '月前'
            } else if (parseInt((nowTime - date) / 86400000) >= 1) {
              return parseInt((nowTime - date) / 86400000) + '天前'
            } else if (parseInt((nowTime - date) / 3600000) >= 1) {
              return parseInt((nowTime - date) / 3600000) + '小时前'
            } else if (parseInt((nowTime - date) / 60000) >= 1) {
              return parseInt((nowTime - date) / 60000) + '分钟前'
            } else {
              return '刚刚'
            }
          }
          for (let i = 0; i < goodList.length; i++) {
            if (goodList[i].tip == 'good') {
              goodList[i].type = self.data.columnsGood[goodList[i].type];
            } else {
              goodList[i].type = self.data.columnsBook[goodList[i].type];
            }
            goodList[i].date = handleTime(goodList[i].date)
          }
          // res.data 是包含以上定义的两条记录的数组
          self.setData({
            goodList: goodList.reverse(),
            pagesIdx: self.data.pagesIdx
          })
        }
      })
  },
  handleCardEntry(e) {
    wx.navigateTo({
      url: '../show/detailed/detailed?_id=' + e.currentTarget.dataset._id + '&_openid='+e.currentTarget.dataset._openid
    }) // 带参跳转
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let self = this;
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: function (res) {
          }, fail: res => {
            wx.reLaunch({
              url: '../index/index'
            })
          }
        })
      },
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  onChange: function (event) {
    let self = this;
    if (!event.detail.index){
      substance.where({
        free: null
      }).get({
        success: res => {
          let goodList = res.data
          let handleTime = (date) => {
            let nowTime = (new Date()).valueOf()
            if (parseInt((nowTime - date) / 2592000000) >= 1) {
              return parseInt((nowTime - date) / 2592000000) + '月前'
            } else if (parseInt((nowTime - date) / 86400000) >= 1) {
              return parseInt((nowTime - date) / 86400000) + '天前'
            } else if (parseInt((nowTime - date) / 3600000) >= 1) {
              return parseInt((nowTime - date) / 3600000) + '小时前'
            } else if (parseInt((nowTime - date) / 60000) >= 1) {
              return parseInt((nowTime - date) / 60000) + '分钟前'
            } else {
              return '刚刚'
            }
          }
          for (let i = 0; i < goodList.length; i++) {
            if (goodList[i].tip == 'good') {
              goodList[i].type = self.data.columnsGood[goodList[i].type];
            } else {
              goodList[i].type = self.data.columnsBook[goodList[i].type];
            }
            goodList[i].date = handleTime(goodList[i].date)
          }
          // res.data 是包含以上定义的两条记录的数组
          self.setData({
            goodList: goodList.reverse()
          })
        }
      })
    }else{
      substance.where({
        free: "免费赠送"
      }).get({
        success: res => {
          console.log(res)
          let freelist = res.data
          let handleTime = (date) => {
            let nowTime = (new Date()).valueOf()
            if (parseInt((nowTime - date) / 2592000000) >= 1) {
              return parseInt((nowTime - date) / 2592000000) + '月前'
            } else if (parseInt((nowTime - date) / 86400000) >= 1) {
              return parseInt((nowTime - date) / 86400000) + '天前'
            } else if (parseInt((nowTime - date) / 3600000) >= 1) {
              return parseInt((nowTime - date) / 3600000) + '小时前'
            } else if (parseInt((nowTime - date) / 60000) >= 1) {
              return parseInt((nowTime - date) / 60000) + '分钟前'
            } else {
              return '刚刚'
            }
          }
          for (let i = 0; i < freelist.length; i++) {
           
            if (freelist[i].tip == 'good') {
              freelist[i].type = self.data.columnsGood[freelist[i].type];
            } else {
              freelist[i].type = self.data.columnsBook[freelist[i].type];
            }
            freelist[i].date = handleTime(freelist[i].date)
          }
          // res.data 是包含以上定义的两条记录的数组
          self.setData({
            freelist: freelist.reverse()
          })
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})